/*
   Copyright (c) 2016-2017 Slamtec Co., Ltd. All Rights Reserved.
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
       http://www.apache.org/licenses/LICENSE-2.0
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package com.csjbot.weedle_auto.Slam.data

import android.graphics.Point
import android.graphics.Rect
import android.graphics.RectF

import com.slamtec.slamware.geometry.PointF
import com.slamtec.slamware.geometry.Size
import com.slamtec.slamware.robot.Map

class MapDataCache {

    private var lastUpdateMapArea: RectF? = null
    private var lastUpdateLogicalArea: Rect? = null

    private var baseCoordinate: PointF? = null
    private var basePixel: Point? = null
    var resolution: PointF? = null
        private set
    private var dataStore: MapDataStore? = null

    init {
        dataStore = MapDataStore()
        resolution = PointF()
        baseCoordinate = PointF()
    }

    val cachedArea: RectF
        get() {
            val size = dimensionToSize(dataStore!!.area!!.width(), dataStore!!.area!!.height())
            return RectF(baseCoordinate!!.x, baseCoordinate!!.y,
                    baseCoordinate!!.x + size.x, baseCoordinate!!.y + size.y)
        }

    val dimension: Point
        get() = Point(dataStore!!.area!!.width(), dataStore!!.area!!.height())

    @Synchronized
    fun update(map: Map?) {
        if (map == null) {
            return
        }
        val mapArea = map.mapArea

        if (dataStore!!.isEmpty) {
            baseCoordinate = translate(map.origin)
            resolution = translate(map.resolution)
        }

        val logicalArea = physicalAreaToLogicalArea(mapArea)

        if (dataStore!!.isEmpty) {
            basePixel = Point(logicalArea.left, logicalArea.top)
        }

        dataStore!!.expandArea(Rect(logicalArea))

        lastUpdateMapArea = mapArea
        lastUpdateLogicalArea = logicalArea

        val updateSize = translate(map.dimension)
        if (logicalArea.width() < updateSize.x) {
            updateSize.x = logicalArea.width()
        }
        if (logicalArea.height() < updateSize.y) {
            updateSize.y = logicalArea.height()
        }

        val mapRawData = map.data
        val src = ByteArray(map.dimension.width)
        val rect = Rect()

        for (y in 0..updateSize.y - 1) {
            val left = logicalArea.left
            val top = logicalArea.bottom - y - 1
            val right = logicalArea.left + updateSize.x
            val bottom = top + 1
            rect.set(left, top, right, bottom)
            System.arraycopy(mapRawData, map.dimension.width * y, src, 0, map.dimension.width)
            dataStore!!.update(rect, src)
        }
    }

    @Synchronized
    fun fetch(area: Rect, buffer: ByteArray) {
        dataStore!!.fetch(area, buffer)
    }

    @Synchronized operator fun get(x: Int, y: Int): Byte {
        return dataStore!!.get(x, y)
    }

    @Synchronized
    fun clear() {
        dataStore!!.clear()
    }

    val isEmpty: Boolean
        @Synchronized get() = dataStore!!.isEmpty

    fun pixelToCoordinate(pixel: Point): PointF {
        val offset = PointF(pixel.x * resolution!!.x, (-pixel.y - 1) * resolution!!.y)
        return PointF(baseCoordinate!!.x + offset.x, baseCoordinate!!.y + offset.y)
    }

    fun coordinateToPixel(x: Float, y: Float): Point {
        val size = sizeToDimension(x - baseCoordinate!!.x, y - baseCoordinate!!.y)
        return Point(size.x, -size.y - 1)
    }

    fun dimensionToSize(width: Int, height: Int): PointF {
        return PointF(width * resolution!!.x, height * resolution!!.y)
    }

    fun sizeToDimension(x: Float, y: Float): Point {
        return Point(Math.round(x / resolution!!.x), Math.round(y / resolution!!.y))
    }

    fun physicalAreaToLogicalArea(rect: RectF): Rect {
        val logicalBasePoint = coordinateToPixel(rect.left, rect.top)
        val logicalSize = sizeToDimension(rect.width(), rect.height())

        val left = logicalBasePoint.x
        val top = logicalBasePoint.y - logicalSize.y + 1
        val right = left + logicalSize.x
        val bottom = top + logicalSize.y

        return Rect(left, top, right, bottom)
    }

    companion object {
        private val TAG = "MapDataCache"

        private fun translate(pt: PointF): PointF {
            return PointF(pt.x, pt.y)
        }

        private fun translate(sz: Size): Point {
            return Point(sz.width, sz.height)
        }
    }
}
